/********************************************************************
	Rhapsody	: 7.5 
	Login		: kbe
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: ReactorServer
//!	Generated Date	: Thu, 27, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\ReactorServer.cpp
*********************************************************************/

//## auto_generated
#include "ReactorServer.h"
//## dependency AlarmEvent
#include "AlarmEvent.h"
//## dependency LogEvent
#include "LogEvent.h"
//## dependency PatientEvent
#include "PatientEvent.h"
//## dependency Reactor
#include "Reactor.h"
//## dependency Acceptor
#include "Acceptor.h"
//## dependency LF_EventHandler
#include "LF_EventHandler.h"
//## dependency ThreadPool
#include "ThreadPool.h"
//## auto_generated
#include <oxf\omthread.h>
//## package Default

//## class ReactorServer
ReactorServer::ReactorServer(IOxfActive* theActiveContext) {
    setActiveContext(theActiveContext, false);
    itsReactor = NULL;
    initStatechart();
}

ReactorServer::~ReactorServer() {
    cleanUpRelations();
}

bool ReactorServer::startBehavior() {
    bool done = false;
    done = OMReactive::startBehavior();
    return done;
}

void ReactorServer::initStatechart() {
    rootState_subState = OMNonState;
    rootState_active = OMNonState;
}

void ReactorServer::CreateEventHandler() {
    //#[ operation CreateEventHandler()
    #if 1
    #define DEFAULT_PORT_LOG		27015
    #define DEFAULT_PORT_PATIENT	27016
    #define DEFAULT_PORT_ALARM		27017  
    
    	unsigned long address = 0x7F000001;   
    	                                                  
    	ThreadPool *threadPool = new ThreadPool(itsReactor);
    	                                                 
    	// Test with all event services (Log, Patient and Alarm)
    	INET_Addr *logAddr = new INET_Addr(DEFAULT_PORT_LOG, address);
    	Acceptor<LogEvent, SOCK_Acceptor> *logAcceptor = new Acceptor<LogEvent, SOCK_Acceptor>(itsReactor, logAddr, threadPool, false);
        LF_EventHandler *LFlogAcceptor = new LF_EventHandler(itsReactor, logAcceptor, threadPool, ACCEPT_EVENT);
    
    	INET_Addr *patientAddr = new INET_Addr(DEFAULT_PORT_PATIENT, address);	
    	Acceptor<PatientEvent, SOCK_Acceptor> *patientAcceptor = new Acceptor<PatientEvent, SOCK_Acceptor>(itsReactor, patientAddr, threadPool, false);
        LF_EventHandler *LFpatientAcceptor = new LF_EventHandler(itsReactor, patientAcceptor, threadPool, ACCEPT_EVENT);
    
    	INET_Addr *alarmAddr = new INET_Addr(DEFAULT_PORT_ALARM, address);
    	Acceptor<AlarmEvent, SOCK_Acceptor> *alarmAcceptor = new Acceptor<AlarmEvent, SOCK_Acceptor>(itsReactor, alarmAddr, threadPool, false);   
        LF_EventHandler *LFalarmAcceptor = new LF_EventHandler(itsReactor, alarmAcceptor, threadPool, ACCEPT_EVENT);
        
    #else
    	int h = 0;      
    	itsReactor->registerHandler(*(new AlarmEvent(itsReactor, (void *)++h)), READ_EVENT);
    	itsReactor->registerHandler(*(new LogEvent(itsReactor, (void *)++h)), READ_EVENT);
    	itsReactor->registerHandler(*(new PatientEvent(itsReactor, (void *)++h)), READ_EVENT);
    	itsReactor->registerHandler(*(new LogAcceptor(itsReactor, (void *)++h)), ACCEPT_EVENT);
    #endif
    //#]
}

void ReactorServer::Initialize() {
    //#[ operation Initialize()
    	WSADATA wsa_data;
    	WORD version_requested = MAKEWORD(2, 2);
    	int error = WSAStartup(version_requested, &wsa_data);
    	if (error != 0) printf("WSAStartup error \n");
    	itsReactor = Reactor::instance(); 
    
    //#]
}

Reactor* ReactorServer::getItsReactor() const {
    return itsReactor;
}

void ReactorServer::cleanUpRelations() {
    if(itsReactor != NULL)
        {
            itsReactor = NULL;
        }
}

void ReactorServer::SimulateEvents() {
    //#[ operation SimulateEvents()
    while (1)
    	itsReactor->handleEvents();
    //#]
}

void ReactorServer::rootState_entDef() {
    {
        rootState_subState = Idle;
        rootState_active = Idle;
    }
}

IOxfReactive::TakeEventStatus ReactorServer::rootState_processEvent() {
    IOxfReactive::TakeEventStatus res = eventNotConsumed;
    switch (rootState_active) {
        case Idle:
        {
            if(IS_EVENT_TYPE_OF(Start_Default_id))
                {
                    //#[ transition 1 
                    Initialize();
                    //#]
                    rootState_subState = WaitRegisterHandles;
                    rootState_active = WaitRegisterHandles;
                    res = eventConsumed;
                }
            
        }
        break;
        case WaitRegisterHandles:
        {
            if(IS_EVENT_TYPE_OF(Register_Default_id))
                {
                    //#[ transition 2 
                    CreateEventHandler();
                    //#]
                    rootState_subState = HandleEvents;
                    rootState_active = HandleEvents;
                    res = eventConsumed;
                }
            
        }
        break;
        case HandleEvents:
        {
            if(IS_EVENT_TYPE_OF(Simulate_Default_id))
                {
                    //#[ transition 3 
                    SimulateEvents();
                    //#]
                    rootState_subState = HandleEvents;
                    rootState_active = HandleEvents;
                    res = eventConsumed;
                }
            
        }
        break;
        default:
            break;
    }
    return res;
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\ReactorServer.cpp
*********************************************************************/
